package com.example.myapplication.tack_27;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_27.data.Data;
import com.example.myapplication.utils.OkHttpTool;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;


public class DataAnalysisActivity extends AppCompatActivity {
    private ImageView exit;
    private BarChart barChart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_27_activity_data_analysis);
        init();
        initExit();
        getData();
    }

    private void getData() {

        String url = "prod-api/api/movie/press/press/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, Data.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                Data data =(Data)obj;
                if(data.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            DataAdapter(data.getRows());
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void DataAdapter(List<Data.RowsDTO> rows) {
        if(rows == null){
            return;
        }

        ArrayList<BarEntry> entries = new ArrayList<>();
        String[] str = new String[5];
        for(int i = 0;i < 5;i++){
            entries.add(new BarEntry(i,rows.get(i).getLikeNum(),rows.get(i).getTitle()));
            str[i] = rows.get(i).getTitle();
        }
        // 获取柱状图（barChart）的X轴（xAxis）
        XAxis xAxis = barChart.getXAxis();
        // 设置横坐标文本样式
        xAxis.setValueFormatter(new IndexAxisValueFormatter(str));
        barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        barChart.getXAxis().setGranularity(1f);
        barChart.getXAxis().setGranularityEnabled(true);


        BarDataSet dataSet = new BarDataSet(entries, "新闻点赞数据分析");
        dataSet.setColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW,Color.GRAY);

        BarData barData = new BarData(dataSet);
        barChart.setData(barData);

        //表示禁用右侧的坐标轴
        barChart.getAxisRight().setEnabled(false);
        //表示设置Y轴的动画效果，动画持续时间为1000毫秒。这样设置可以使得图表在显示的时候具有动态的效果，增加用户的交互性和可视性
        barChart.animateY(1000);


        //最后调用invalidate()方法刷新图表
        barChart.invalidate();
    }


    private void initExit() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataAnalysisActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        barChart = findViewById(R.id.chart);
    }
}